window._isIE = (navigator.appName == "Microsoft Internet Explorer");
if(window._isIE) {
    if(navigator.userAgent.indexOf("Opera")>-1) window._isIE = null;
    if(navigator.userAgent.indexOf("Windows NT 6.0")>-1) window._isVista = true;
    else window._isVista = false;
}
else {
    if(navigator.userAgent.indexOf("Gecko")==-1) window._isIE = null;
}

function query(name){ 
    var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");  
    var r = window.location.search.substr(1).match(reg);   
    if (r != null) return unescape(r[2]); 
    return "";
}

/**无刷新提交表单**/
//必须先引入jquery.form.js
function ajaxform(form, container, startfunc, endfunc) {
    var options = {
        target:        container,   //容器
        beforeSubmit:	function(formData) {
            //            alert(formData.val());
            if(startfunc) return startfunc();
        },
        success:	function(msg) {
            if(endfunc) return endfunc(msg);
        }
    };
    $(form).ajaxSubmit(options);
    return false;
}

/**网址添加到收藏夹**/
function myAddPanel(url, title, desc) {
    if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))	//Gecko
    {
        window.sidebar.addPanel(title, url, desc);
    }
    else	//IE
    {
        window.external.AddFavorite(url,title);
    }
}

function setHome(url, title) {
    if(confirm(title)) {
        this.setHomePage(url);
    }
}

/**获取checkbox条目的value值, chkHidden为true是则获取itemid的值**/
function getItemValue(class_id, chkHidden) {
    if(!chkHidden) {
        var ids = $.map($('.' +class_id+':checked'), function(i) {
            return i.value
        }).toString();
    }
    else {
        var ids = $.map($('.' +class_id+':checked'), function(i) {
            return $(i).attr('itemid')
        }).toString();
    }
    return ids;
}

/**检测控件有无被选中**/
function chkItem(class_id, msg) {
    if(msg == undefined) msg = '您还未选择条目，不能操作';
    if($('.' +class_id+ ':checked').length == 0) {
        alert(msg);
        return false;
    }
    return true;
}

/**全选全不选**/
//var ischeck = false;
function chkUnchkAll(click_id, class_id, ischeck) {
    if($('#' + click_id).attr('type') !='checkbox') {
        if(!ischeck) {
            $('.' + class_id).each(function(x) {
                this.checked = true;
            });
//            ischeck = true;
        }
        else {
            $('.' + class_id).each(function() {
                this.checked = false;
            });
//            ischeck = false;
        }
    }
    else {
        if($('#' + click_id)[0]) {
            if($('#' + click_id)[0].checked) {
                $('.' + class_id).each(function(x) {
                    this.checked = true;
                });
            }
            else {
                $('.' + class_id).each(function() {
                    this.checked = false;
                });
            }
        }

        $('#' + click_id).each(function() {
            $(this).click(function() {
                if(this.checked) {
                    $('.' + class_id).each(function(x) {
                        this.checked = true;
                    });
                }
                else {
                    $('.' + class_id).each(function() {
                        this.checked = false;
                    });
                }
            })
        })
    }

}

/**分页导航函数*/
/*
类型		参数			解释					例子
-------------------------------------------------------
int		records		记录条数 				20
int		size 		每页显示数		 	10 
int		page 		当前页码				5
string 	container 	装载导航的容器		.pager
string 	qstring 	查询字符串 			&a=?&b=?&etc=? | return myfunc([p]);
bool	ajax 		是否调用js函数		true
int 	num  		显示分页码的个数		5
*/
function pager(records, size, page, container, qstring, ajax, num, jump) {
    (function($) {
        if(!ajax) ajax = false;
        if(qstring == undefined) qstring = '';
        if(records == undefined) records = 0;
        if(size == undefined) size = 0;
        if(num == undefined) num = 5;
        var pager = new PAGER('首页', '上页', '下页', '尾页', '');
        if(jump != undefined && jump == false) pager.jump = false;
        pager.NumCount = num;
        pager.records = records;
        pager.pagesize = size;
        pager.ajax = ajax;
        if(page == 'this.value') {
            pager.page = 2;
        }
        else {
            pager.page = page;
        }
        pager.Qstring = qstring;
        $('.' + container).html(pager.navigation());
        return pager;
    })(jQuery);
}

/**图片按比例缩放**/
function DrawImage(ImgD, iwidth, iheight){
    var image = new Image();
    image.src = ImgD.src;
    if(iwidth && image.width > 0 && image.height > 0){
        if(image.width > iwidth) {
            ImgD.width = iwidth;
            ImgD.height = (image.height * iwidth) / image.width;
        }
    }
    else {
        if(image.height > iheight) {
            ImgD.height = iheight;
            ImgD.width = (image.width * iheight) / image.height;
        }
    }
}

function setW(str, container) {
    if(!container) container = '#warning';
    setWposition();
    if(str) {
        $(container).html('<span class="warning_text">' +str+ '</span>').show();
    }
    else {
        $(container).hide();
    }
}

//设置信息提示框的位置(全屏遮档)
function setWposition(container) {
    if(!container) container = '#warnning';
    var height = document.body.scrollHeight;
    var pad = height/2 - 50;
    $(container).css({
        top:0,
        width:screen.width-18,
        height:height,
        'padding-top': pad
    });
}

function loadByAction(options, container, action, U, txt, func, hidden) {
    var ref = '';
    if(U != undefined) {
        ref = U;
    }
    else {
        ref = this.URL;
    }

    if (txt) setW(txt);
    $.post(ref + action, options, function(data) {
        if(!func) {
            if(container) {
                if(!hidden) {
                    $(container).html(data).show();
                }
                else {
                    $(container).html(data);
                }
            }
        }
        else {
            func(data, this);
        }
        setW();
    });
}

function toJSON(data) {
    eval('var json='+data);
    return json;
}

/**
 * This js provide a simple way to operate cookie in javascript
 * function getCookie(name);
 * function setCookie(name,value[,expireDays]);
 * function deleteCookie(name);
 */
function getCookie(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return "";
}

function setCookie(name, value)
{
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var expDay = (argc > 2) ? argv[2] : -1;
    try
    {
        expDay = parseInt(expDay);
    }
    catch(e)
    {
        expDay = -1;
    }
    if(expDay < 0) {
        setCookieVal(name, value);
    } else {
        var expDate = new Date();
        // The expDate is the date when the cookie should expire, we will keep it for a month
        expDate.setTime(expDate.getTime() + (expDay * 24 * 60 * 60 * 1000));
        setCookieVal(name, value, expDate);
    }
}

function deleteCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    // This cookie is history
    var cval = getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function getCookieVal(offset)
{
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function setCookieVal(name, value)
{
    var argv = setCookieVal.arguments;
    var argc = setCookieVal.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
    ((expires == null || expires < 0) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}